﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YJKRevitView
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdBelowFloorView : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			try
			{
				Document document = cmdData.Application.ActiveUIDocument.Document;
				Autodesk.Revit.DB.View activeView = cmdData.Application.ActiveUIDocument.ActiveView;
				Level genLevel = cmdData.Application.ActiveUIDocument.ActiveView.GenLevel;
				List<ViewPlan> list = (from ViewPlan c in new FilteredElementCollector(document).OfClass(typeof(ViewPlan))
				orderby c.GetYJKLevel() descending
				select c).ToList<ViewPlan>();
				List<ViewPlan> list2 = new List<ViewPlan>();
				foreach (ViewPlan viewPlan in list)
				{
					int num = activeView.get_Parameter(BuiltInParameter.VIEW_DISCIPLINE).AsInteger();
					int num2 = viewPlan.get_Parameter(BuiltInParameter.VIEW_DISCIPLINE).AsInteger();
					string a = activeView.get_Parameter(BuiltInParameter.VIEW_FAMILY).AsString();
					string b = viewPlan.get_Parameter(BuiltInParameter.VIEW_FAMILY).AsString();
					if (num == num2 && a == b)
					{
						list2.Add(viewPlan);
					}
				}
				ViewJumpControler.Sort(list2, false);
				double num3 = double.MaxValue;
				foreach (ViewPlan viewPlan2 in list2)
				{
					Level genLevel2 = viewPlan2.GenLevel;
					if (genLevel2 != null && ViewJumpControler.LessThan(genLevel2.Elevation, genLevel.Elevation))
					{
						double num4 = Math.Abs(genLevel2.Elevation - genLevel.Elevation);
						if (num4 != 0.0 && ViewJumpControler.LessThan(num4, num3))
						{
							num3 = num4;
						}
					}
				}
				List<ViewPlan> list3 = new List<ViewPlan>();
				List<string> list4 = new List<string>();
				foreach (ViewPlan viewPlan3 in list2)
				{
					Level genLevel3 = viewPlan3.GenLevel;
					if (genLevel3 != null && ViewJumpControler.IsEqual(genLevel3.Elevation, genLevel.Elevation - num3))
					{
						list3.Add(viewPlan3);
						list4.Add(viewPlan3.Name);
					}
				}
				if (list3.Count == 0)
				{
					YJKMessageBox.Show("已到最底层。");
				}
				else if (list3.Count == 1)
				{
					cmdData.Application.ActiveUIDocument.ActiveView = list3[0];
				}
				else
				{
					SelectViewForm selectViewForm = new SelectViewForm(list4);
					if (selectViewForm.ShowDialog() != DialogResult.OK)
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					cmdData.Application.ActiveUIDocument.ActiveView = list3[selectViewForm.SelectIndex];
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
			}
			return base.YJKExcute(cmdData, ref message, elements);
		}
	}
}
